// pages/gatherHouse/radarHouse/radarHouse.js
import p from "../../../utils/platform";
import api from "../../../common/api";
import { moveHouseLog } from "../../../common/log";
import { menuHandler } from "../../../common/menuHandler";
import { HOUSE_RADAR } from "../../../common/menuId";

const computedBehavior = require("miniprogram-computed");

const app = getApp();
const URL_MAP = [
  // "58.com",
  "ke.com",
  "lianjia.com",
  // "anjuke.com"
];

const errorMsg = {
  5001: "请检查网站是否正确",
};

Page({
  behaviors: [computedBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    subscribe: false,
    isFailPopShow: false,
    isRadarShow: false,
    citys: "",
    list: [
      {
        name: "真房源网",
        title: "“真房源网”网站房源详情页点击“分享此房源”即是弹框二维码",
        img: "https://api.shulanfd.com/aist-filesvr-web/api/files/5f2d40ba4f2e4a678dabf7badc778c9c",
      },
      {
        name: "真房源网",
        title: "“真房源网”网站房源详情页点击“分享此房源”即是弹框二维码",
        img: "https://api.shulanfd.com/aist-filesvr-web/api/files/73e6c9fdf88745f98cd704c8fe5c91dd",
      },
      // {name: "58同城",title: "“58同城”网站房源详情页右下方“手机看房源”即是房源二维码", img: "https://api.shulanfd.com/aist-filesvr-web/api/files/1902b9c43281450fb199b81accb3d382"},
      // {name: "安居客",title: "“安居客”网站房源详情页右下方“手机看房源”即是房源二维码", img: "https://api.shulanfd.com/aist-filesvr-web/api/files/29965d1eb50340afb10bdf5632b5f80b"},
    ],
    textStatue: "1", //没有相关房源的原因
  },
  computed: {
    titlesName(data) {
      // return data.list.map(v => v.name).join("、")
      return "端口网站二手房";
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.loginPromise.then(() => {
      // options.subscribe=true;
      // options.url='https://m.ke.com/cwap/guide/hsdetail?cityId=310000&hsId=107103048772';
      // options.url='https://m.ke.com/cwap/guide/hsdetail?cityId=310000&hsId=1071035082111';
      if (options.subscribe) {
        this.setData({
          subscribe: true,
        });
      }
      if (options.url) {
        options.url = decodeURIComponent(options.url);
        this.fetchRadar(options.url);
      }
      // ROOMRADAR
      api.getCityByModule({ moduleCode: "ROOMRADAR" }, ({ data }) => {
        data.content = data.content || [];
        this.setData({
          citys: data.content.join("、"),
        });
      });
    });
  },

  fetchRadar(result) {
    let that = this;
    this.setData({
      isRadarShow: true,
    });
    api.roomRadar({ url: result }, ({ data }) => {
      this.setData({
        isRadarShow: false,
      });

      if (data.isSuccess) {
        let content = data.content;
        // content = {
        //   buildSize: 92,
        //   buildingNumList: ["3期5栋", "30期88栋", "1期9栋"],
        //   city: "上海",
        //   hall: 2,
        //   hdResblockId: "eb45a90053554222ad4caf661e45d374",
        //   lat: 30.738136,
        //   lng: 121.329036,
        //   resblockName: "御景龙庭",
        //   room: 2,
        //   address: "龙华路100号",
        // }

        content.buildingNumList = content.buildingNumList || [];
        if (content.buildingNumList.length > 0) {
          app.globalData.radarInfo = {
            url: result,
            content: content,
          };

          // 使用次数的扣除由后端做
          // api.getMemberPower({
          //   menuId: HOUSE_RADAR,
          //   agentId: app.globalData.sessionUser.id,
          // });
          if (this.data.subscribe) {
            p.redirectTo({ url: "/pages/gatherHouse/radarSuccess/radarSuccess?subscribe=true" });
          } else {
            p.navigateTo({ url: "/pages/gatherHouse/radarSuccess/radarSuccess" });
          }
        } else {
          this.setData({
            isFailPopShow: true,
            textStatue: content.textStatue,
          });
        }
      } else {
        if (data.errorMessage == "暂时不支持该小区") {
          this.setData({
            isFailPopShow: true,
          });
        } else {
          wx.showModal({
            content: data.errorMessage || "暂不支持该类型的链接",
            showCancel: false,
            confirmText: "确定",
            success(res) {
              if (res.confirm) {
                if (that.data.subscribe) {
                  wx.navigateBack({
                    delta: 1,
                  });
                }
              }
            },
          });
        }
      }
    });
  },

  handleScan() {
    menuHandler.call(this, HOUSE_RADAR).then(() => {
      wx.scanCode({
        success: (res) => {
          const result = res.result;
          console.log("扫码===", result);
          let bool = URL_MAP.find((item) => {
            return result.indexOf(item) > -1;
          });

          if (bool) {
            this.fetchRadar(result);
          } else {
            p.showModal({
              content: `非常抱歉，暂时不支持该网站的房雷达功能`,
              showCancel: false,
            });
          }
        },

        fail: (res) => {},
      });
    }).catch(() => {});
  },
  canHandleScan() {
    app
      .mobileLogin(null, "房雷达")
      .then(() => {
        // p.navigateTo({ url: '/pages/gatherHouse/radarHouse/radarHouse',})
        this.handleScan();
      })
      .catch(() => {
        app.globalData.afterMobileAuthorCallBack = () => {
          p.redirectTo({ url: "/pages/gatherHouse/radarHouse/radarHouse" });
          // this.handleScan();
        };
      });
  },
  handleClosePop() {
    this.setData({
      isFailPopShow: false,
    });
  },
  back() {
    wx.navigateBack({
      delta: 1,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
});
